﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace WQDSkin.Forms.Component
{
    public class WQDEditPage : Control
    {
        private Dictionary<string, Frame> pageList = new Dictionary<string, Frame>(); //子页集合
        private Grid mainContain;

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e); 
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            Frame mainFrame = (Frame)this.Template.FindName("mainFrame", this); 
            mainFrame.LoadCompleted += LoadCompleted;
            mainFrame.DataContextChanged += MainFrame_DataContextChanged;
            this.pageList.Add("mainFrame", mainFrame);

            this.mainContain = (Grid)this.Template.FindName("mainContain", this);
        }

        public void GoBack()
        {
            //隐藏其它所有的
            foreach (KeyValuePair<string, Frame> keyValue in this.pageList)
            {
                if (keyValue.Key != "mainFrame")
                {
                    keyValue.Value.Visibility = Visibility.Collapsed;
                }
                else
                {
                    keyValue.Value.Visibility = Visibility.Visible;
                }
            }
        }

        public void Navigate(string uriString, object model)
        {
            //隐藏其它所有的Frame
            foreach (KeyValuePair<string, Frame> keyValue in this.pageList)
            {
                if (keyValue.Key != uriString)
                {
                    keyValue.Value.Visibility = Visibility.Collapsed;
                } 
            }

            if (this.pageList.ContainsKey(uriString))
            {
                //显示导航的Frame
                Frame frame = this.pageList[uriString];

                frame.DataContext = model;
                frame.Visibility = Visibility.Visible;
                frame.Refresh();
            }
            else
            {
                Frame frame = new Frame();
                this.mainContain.Children.Add(frame);

                 
                frame.LoadCompleted += LoadCompleted;
                frame.DataContextChanged += MainFrame_DataContextChanged;
                frame.Navigate(new Uri(uriString + ".xaml", UriKind.Relative)); 
                frame.DataContext = model;
                  
                this.pageList.Add(uriString, frame);
            } 
        }

        public void CallBack(object model)
        {
            foreach (KeyValuePair<string, Frame> keyValue in this.pageList)
            {
                if (keyValue.Key != "mainFrame")
                {
                    keyValue.Value.Visibility = Visibility.Collapsed; 
                }
                else
                {
                    keyValue.Value.Visibility = Visibility.Visible;
                    (keyValue.Value.Content as BasePage).CallBack(model);
                }
            }
        }

        private void LoadCompleted(object sender, NavigationEventArgs e)
        { 
             UpdateFrameDataContext(sender);
        }

        private void MainFrame_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        { 
            UpdateFrameDataContext(sender);
        }

        /// <summary>
        ///  Frame 的 DataContext 不能被 Page 继承,需要做进一步的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateFrameDataContext(object sender)
        {
            Frame frame = (Frame)sender;
            var content = frame.Content as BasePage;
            if (content == null)
            {
                return;
            }
            content.Contain = this;
            content.DataContext = frame.DataContext;
        }

        /// <summary>
        /// 主页
        /// </summary>
        public Uri MainUri
        {
            get { return (Uri)GetValue(MainUriProperty); }
            set { SetValue(MainUriProperty, value); }
        }

         public static readonly DependencyProperty MainUriProperty =
            DependencyProperty.Register("MainUri", typeof(Uri), typeof(WQDEditPage), 
                new PropertyMetadata(null));


    }
}
